package com.sfh.sfhshopweb.controller.admin;

import com.sfh.sfhshopweb.dto.ResponseResult;
import com.sfh.sfhshopweb.dto.WebPage;
import com.sfh.sfhshopweb.entity.Category;
import com.sfh.sfhshopweb.entity.Goods;
import com.sfh.sfhshopweb.enums.BooleanEnums;
import com.sfh.sfhshopweb.service.ICategoryService;
import com.sfh.sfhshopweb.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/goods")
@Api(tags = "系统管理-商品管理")
public class AdminGoodsController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IGoodsService goodsService;

    @PostMapping("/getCategoryTree")
    @ApiOperation(value = "获取商品分类树")
    public ResponseResult<List<Category>> getCategoryTree() {
        List<Category> category = categoryService.getCategoryTree();
        return ResponseResult.success(category);
    }

    @PostMapping("/getTopCategory")
    @ApiOperation(value = "获取一级商品分类")
    public ResponseResult<List<Category>> getCategory() {
        List<Category> category = categoryService.getTopCategory();
        return ResponseResult.success(category);
    }

    @PostMapping("/getCategoryOne")
    @ApiOperation(value = "获取商品分类详情")
    public ResponseResult<Category> getCategoryOne(@RequestParam("id") Integer id) {
        Category category = categoryService.getOne(id);
        return ResponseResult.success(category);
    }

    @PostMapping("saveOrUpdateCategory")
    @ApiOperation(value = "保存或更新商品分类")
    public ResponseResult<Boolean> saveOrUpdateCategory(@RequestBody Category category) {
        boolean b = categoryService.saveOrUpdate(category);
        return ResponseResult.success(b);
    }

    @PostMapping("/page")
    @ApiOperation(value = "商品分页查询")
    public ResponseResult<WebPage<Goods>> page(@RequestBody WebPage<Goods> pageDTO) {
        return ResponseResult.success(goodsService.queryPage(pageDTO));
    }

    @PostMapping("/enableGoods")
    @ApiOperation(value = "上架/下架商品")
    public ResponseResult<Boolean> enableGoods(@RequestParam("id") Long id, @RequestParam("enable") Integer enable) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setEnable(BooleanEnums.getEnumByCode(enable));
        boolean b = goodsService.updateById(goods);
        return ResponseResult.success(b);
    }

    @PostMapping("/getOne")
    @ApiOperation(value = "获取商品详情")
    public ResponseResult<Goods> getGoods(String id) {
        Goods goods = goodsService.getById(id);
        return ResponseResult.success(goods);
    }

    @PostMapping("/saveOrUpdateGoods")
    @ApiOperation(value = "保存或更新商品")
    public ResponseResult<Boolean> saveOrUpdateGoods(@RequestBody Goods goods) {
        boolean b = goodsService.saveOrUpdate(goods);
        return ResponseResult.success(b);
    }

    @PostMapping("/deleteGoods")
    @ApiOperation(value = "删除商品")
    public ResponseResult<Boolean> deleteGoods(@RequestParam("id") Long id) {
        boolean b = goodsService.deleteGoods(id);
        return ResponseResult.success(b);
    }
}
